// 配置路由的地方
// 引入vue
import Vue from 'vue';
import VueRouter from 'vue-router';
// 使用插件
// 自定义插件
Vue.use(VueRouter);
// 引入store
import store from '@/store';
// 引入路由组件
import Home from '@/pages/Home'
import SeekFamily from '@/pages/SeekFamily'
import Login from '@/pages/Login'
import Register from '@/pages/Register'
import Board from '@/pages/Board'
import Test from '@/pages/Test'
// 先把VueRouter原型对象的push，先保存一份
let originPush = VueRouter.prototype.push;
let originReplace = VueRouter.prototype.replace;
// 重写push与replace方法
// 第一个参数：告诉原来的push方法，往哪里跳转（传递哪些参数）
VueRouter.prototype.push = function (location, resolve, reject) {
    if (resolve && reject) {
        originPush.call(this, location, resolve, reject);
    } else {
        originPush.call(this, location, () => {}, () => {});
    }
}
VueRouter.prototype.replace = function (location, resolve, reject) {
    if (resolve && reject) {
        originReplace.call(this, location, resolve, reject);
    } else {
        originReplace.call(this, location, () => {}, () => {});
    }
}
// 配置路由,对外暴露VueRouter类的实例
let router = new VueRouter({
    routes: [{
            path: "/home",
            component: Home,
            // 路由元信息，展不展示
            meta: {
                show: true
            }
        },
        {
            path: "/SeekFamily/:keyword?",
            component: SeekFamily,
            meta: {
                show: true
            },
            name: "SeekFamily"

        },
        {
            path: "/login",
            component: Login,
            meta: {
                show: true
            }
        },
        {
            path: "/register",
            component: Register,
            meta: {
                show: true
            }
        },
        {
            path: "/board",
            component: Board,
            name: "Board"

        },
        {
            path: "/test",
            component: Test,
            name: 'Test'
        },
        // 重定向：在项目运行时，访问/，立马让他定向到首页
        {
            path: "*",
            redirect: "/Home"
        }
    ]
});
router.beforeEach(async (to, from, next) => {
    // 用户登录了才会有token，未登录一定不会有token
    let token = store.state.user.token;
    console.log(token);
    // 每次刷新前，得保证userInfo一直有
    let name = store.state.user.userInfo.name;
    // 用户已登录
    if(token){
        //已经登录而且还想去登录------不行
        if(to.path=="/login"||to.path=='/register'){
           next('/');
        }else{
          //已经登录了,访问的是非登录与注册---跳到search，home之类的
          //登录了且拥有用户信息放行
          if(name){
            next();
          }else{
            next();
            //登录了且没有用户信息（有刷新操作）
            //在路由跳转之前，派发action获取用户信息，且next放行
            try {
            //  await store.dispatch('getUserInfo');
             next();
            } catch (error) {
              //token失效从新登录
             await store.dispatch('userLogout');
              next('/login')
            }
          }
        }
      }else {
        // 未登录，随心所欲
        next();
    }
})
export default router;
// 这个文件需要执行一次，在main.js中